﻿using blackhouse.NemoBlog.Business;
using blackhouse.WebNemo.Business.Groups;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;

namespace blackhouse.StareKino.Web.Events {

    internal class CinemaNemoBlogPosts : NemoBlogPosts {

        #region Fields

        private int placeId;

        #endregion

        #region Constructors

        public CinemaNemoBlogPosts(int placeId)
            : base(placeId) {
            this.placeId = placeId;
        }

        #endregion

        #region Methods

        public NemoBlogPost[] GetLastPostsByEventDateRange(int postCount, DateTime eventDateFrom, DateTime eventDateTo) {
            return this.GetLastPostsByEventDateRange(0, postCount, eventDateFrom, eventDateTo);
        }

        public NemoBlogPost[] GetLastPostsByEventDateRange(int lastLoadedPostId, int postCount, DateTime eventDateFrom, DateTime eventDateTo) {
            IGroup group = BelongToGroup();
            GroupRecords records = new GroupRecords(group);
            GroupRecordQuery q = new GroupRecordQuery(group);
            q.SetProcedureName("bhp_NemoBlogPosts_GetLastByEventDateRange");
            q.AddProcedureParameter("Count", postCount);
            q.AddProcedureParameter("ModuleId", this.placeId, System.Data.DbType.Int32);
            q.AddProcedureParameter("Language", CultureInfo.CurrentCulture.Name, System.Data.DbType.AnsiStringFixedLength);
            if (lastLoadedPostId > 0)
                q.AddProcedureParameter("LastPostId", lastLoadedPostId, System.Data.DbType.Int32);
            q.AddProcedureParameter("FromDate", eventDateFrom, System.Data.DbType.DateTime);
            q.AddProcedureParameter("ToDate", eventDateTo, System.Data.DbType.DateTime);
            return records.GetRecords<NemoBlogPost>(q);
        }

        public NemoBlogPost[] GetLastPostsOrderByEventDate(int postCount, bool onlyPublished) {
            return this.GetLastPostsOrderByEventDate(0, postCount, onlyPublished);
        }

        public NemoBlogPost[] GetLastPostsOrderByEventDate(int lastLoadedPostId, int postCount, bool onlyPublished) {
            IGroup group = BelongToGroup();
            GroupRecords records = new GroupRecords(group);
            GroupRecordQuery q = new GroupRecordQuery(group);
            q.SetProcedureName("bhp_NemoBlogPosts_GetLastEvents");
            q.AddProcedureParameter("Count", postCount);
            q.AddProcedureParameter("ModuleId", this.placeId, System.Data.DbType.Int32);
            q.AddProcedureParameter("OnlyPublic", onlyPublished, System.Data.DbType.Boolean);
            q.AddProcedureParameter("Language", CultureInfo.CurrentCulture.Name, System.Data.DbType.AnsiStringFixedLength);
            if (lastLoadedPostId > 0)
                q.AddProcedureParameter("LastPostId", lastLoadedPostId, System.Data.DbType.Int32);
            return records.GetRecords<NemoBlogPost>(q);
        }

        #endregion

    }

}